// app.js
App({
  onLaunch(options) {
    console.log('APP-onLaunch');
    console.log(options);

    this.globalData.companyId = options.query.companyId || 1
    this.globalData.parentId = options.query.parentId || 0
    this.globalData.shareId = options.query.shareId || 0

    this.checkLogin()
  },

  checkLogin() {
    var that = this;
    var isLogin = wx.getStorageSync("isXiuZhengLogin");
    if (isLogin) {
      var user = JSON.parse(wx.getStorageSync('user'))
      console.log(user);
      that.globalData.user = user
      that.globalData.openId = user.openId
      that.globalData.userId = user.userId
      that.globalData.userType = user.userType
      that.globalData.usedPoints = user.usedPoints
      that.globalData.userScore = user.userScore
      that.globalData.diamondNum = user.diamondNum
      that.globalData.userBalance = user.userBalance
      console.log(that.globalData);
    }
  },
  getInfo() {
    var that = this;
    return new Promise(function (resolve, reject) {
      console.log("登录");
      // 登录
      wx.login({
        success: res => {
          console.log(res.code);
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var url = that.globalData.url + "scUser/userLogin";
          that.wxRequest(url, {
            code: res.code,
            parentId: that.globalData.parentId,
            companyId: that.globalData.companyId,
            shareId: that.globalData.shareId
          }, res => {
            console.log(res);
            if (res.flag) {
              if (res.data.user.userName == '' || res.data.user.userName == null) {
                wx.reLaunch({
                  url: '/userPackage/pages/userInfo/userInfo',
                })
              }
              resolve(res);
            } else {
              wx.showToast({
                title: '信息验证失败，请重试',
                icon: 'none'
              })
            }
          }, err => {})
        }
      })
    })
  },



  wxRequestJson(url, data, succFuc, errFuc) {
    wx.request({
      url: url,
      data: data,
      method: 'POST',
      header: {
        'Content-type': 'application/json;charset=utf-8',
      },
      success: res => {
        succFuc(res.data);
      },
      fail: err => {
        errFuc(err);
      }
    })
  },

  wxRequest(url, data, succFuc, errFuc) {
    wx.request({
      url: url,
      data: data,
      method: 'POST',
      header: {
        'Content-type': 'application/x-www-form-urlencoded',
      },
      success: res => {
        succFuc(res.data);
      },
      fail: err => {
        errFuc(err);
      }
    })
  },

  globalData: {
    url: 'http://192.168.0.103:2036/',
    // url: 'https://www.ccclkj.cn:2036/',
    userId: '',
    user: '',
    openId: '',
    userType: '',
    usedPoints: 0, //累计消耗积分
    userScore: 0, //用户积分数
    diamondNum: 0, //钻石数
    userBalance: 0, //账户余额
    companyId: 1,
    parentId: 0, //直接进入
    shareId: 0, //分享人id
  }
})